home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing;
-
- import java.awt.Component;
- import java.awt.Container;
- import java.awt.Dimension;
- import java.awt.Frame;
- import java.awt.Point;
- import java.awt.Rectangle;
- import java.awt.Toolkit;
- import java.awt.event.MouseAdapter;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionListener;
- import java.util.EventObject;
-
- public class ToolTipManager extends MouseAdapter implements MouseMotionListener {
- Timer enterTimer = new Timer(750, new insideTimerAction(this));
- Timer exitTimer;
- Timer insideTimer;
- String toolTipText;
- Point preferredLocation;
- JComponent insideComponent;
- MouseEvent mouseEvent;
- boolean showImmediately;
- static final ToolTipManager sharedInstance = new ToolTipManager();
- Popup tipWindow;
- JToolTip tip;
- Rectangle popupRect = null;
- boolean enabled = true;
- boolean mouseAboveToolTip = false;
- private boolean tipShowing = false;
- private long timerEnter = 0L;
- protected boolean lightWeightPopupEnabled = true;
-
- ToolTipManager() {
- this.enterTimer.setRepeats(false);
- this.exitTimer = new Timer(500, new outsideTimerAction(this));
- this.exitTimer.setRepeats(false);
- this.insideTimer = new Timer(4000, new stillInsideTimerAction(this));
- this.insideTimer.setRepeats(false);
- }
-
- static Frame frameForComponent(Component component) {
- while(!(component instanceof Frame)) {
- component = component.getParent();
- }
-
- return (Frame)component;
- }
-
- public int getDismissDelay() {
- return this.insideTimer.getInitialDelay();
- }
-
- public int getInitialDelay() {
- return this.enterTimer.getInitialDelay();
- }
-
- public int getReshowDelay() {
- return this.exitTimer.getInitialDelay();
- }
-
- void hideTipWindow() {
- if (this.tipWindow != null) {
- this.tipWindow.removeMouseListener(this);
- this.tipWindow.hide();
- this.tipWindow.dispose();
- this.tipWindow = null;
- this.tipShowing = false;
- this.timerEnter = 0L;
- this.tip.getUI().uninstallUI(this.tip);
- this.tip = null;
- this.insideTimer.stop();
- }
-
- }
-
- public boolean isEnabled() {
- return this.enabled;
- }
-
- public boolean isLightWeightPopupEnabled() {
- return this.lightWeightPopupEnabled;
- }
-
- public void mouseDragged(MouseEvent event) {
- }
-
- public void mouseEntered(MouseEvent event) {
- if (!this.tipShowing || this.lightWeightPopupEnabled || System.currentTimeMillis() - this.timerEnter >= 100L) {
- if (((EventObject)event).getSource() != this.tipWindow) {
- JComponent component = (JComponent)((EventObject)event).getSource();
- this.toolTipText = component.getToolTipText(event);
- this.preferredLocation = component.getToolTipLocation(event);
- this.exitTimer.stop();
- Point location = event.getPoint();
- if (location.x >= 0 && location.x < component.getWidth() && location.y >= 0 && location.y < component.getHeight()) {
- if (this.insideComponent != null) {
- this.enterTimer.stop();
- this.insideComponent = null;
- }
-
- ((Component)component).addMouseMotionListener(this);
- this.insideComponent = component;
- if (this.toolTipText != null) {
- this.mouseEvent = event;
- if (this.showImmediately) {
- this.showTipWindow();
- } else {
- this.enterTimer.start();
- }
- }
-
- }
- }
- }
- }
-
- public void mouseExited(MouseEvent event) {
- if (!this.tipShowing || this.lightWeightPopupEnabled || System.currentTimeMillis() - this.timerEnter >= 100L) {
- boolean shouldHide = true;
- if (((EventObject)event).getSource() == this.tipWindow) {
- Container insideComponentWindow = this.insideComponent.getTopLevelAncestor();
- Rectangle b = this.tipWindow.getBounds();
- Point location = event.getPoint();
- location.x += b.x;
- location.y += b.y;
- b = ((Component)insideComponentWindow).getBounds();
- location.x -= b.x;
- location.y -= b.y;
- location = SwingUtilities.convertPoint((Component)null, location, this.insideComponent);
- if (location.x >= 0 && location.x < this.insideComponent.getWidth() && location.y >= 0 && location.y < this.insideComponent.getHeight()) {
- shouldHide = false;
- } else {
- shouldHide = true;
- }
- } else if (((EventObject)event).getSource() == this.insideComponent && this.tipWindow != null) {
- Point location = SwingUtilities.convertPoint(this.insideComponent, event.getPoint(), (Component)null);
- Rectangle bounds = this.insideComponent.getTopLevelAncestor().getBounds();
- location.x += bounds.x;
- location.y += bounds.y;
- bounds = this.tipWindow.getBounds();
- if (location.x >= bounds.x && location.x < bounds.x + bounds.width && location.y >= bounds.y && location.y < bounds.y + bounds.height) {
- shouldHide = false;
- } else {
- shouldHide = true;
- }
- }
-
- if (shouldHide) {
- this.enterTimer.stop();
- if (this.insideComponent != null) {
- this.insideComponent.removeMouseMotionListener(this);
- }
-
- this.insideComponent = null;
- this.toolTipText = null;
- this.mouseEvent = null;
- this.hideTipWindow();
- this.exitTimer.start();
- }
-
- }
- }
-
- public void mouseMoved(MouseEvent event) {
- JComponent component = (JComponent)((EventObject)event).getSource();
- String newText = component.getToolTipText(event);
- Point newPreferredLocation = component.getToolTipLocation(event);
- if (newText == null && newPreferredLocation == null) {
- this.toolTipText = null;
- this.preferredLocation = null;
- this.mouseEvent = null;
- this.hideTipWindow();
- this.enterTimer.stop();
- this.exitTimer.start();
- } else {
- this.mouseEvent = event;
- if ((newText != null && newText.equals(this.toolTipText) || newText == null) && (newPreferredLocation != null && newPreferredLocation.equals(this.preferredLocation) || newPreferredLocation == null)) {
- if (this.tipWindow != null) {
- this.insideTimer.restart();
- } else {
- this.enterTimer.restart();
- }
- } else {
- this.toolTipText = newText;
- this.preferredLocation = newPreferredLocation;
- if (this.showImmediately) {
- this.hideTipWindow();
- this.showTipWindow();
- } else {
- this.enterTimer.restart();
- }
- }
- }
-
- }
-
- public void mousePressed(MouseEvent event) {
- this.hideTipWindow();
- this.enterTimer.stop();
- this.showImmediately = false;
- }
-
- private boolean popupFit(Rectangle popupRectInScreen, Component invoker) {
- if (invoker != null) {
- for(Container parent = invoker.getParent(); parent != null; parent = ((Component)parent).getParent()) {
- if (parent instanceof JFrame || parent instanceof JDialog) {
- return SwingUtilities.isRectangleContainingRectangle(((Component)parent).getBounds(), popupRectInScreen);
- }
-
- if (parent instanceof JApplet) {
- Rectangle r = ((Component)parent).getBounds();
- Point p = ((Component)parent).getLocationOnScreen();
- r.x = p.x;
- r.y = p.y;
- return SwingUtilities.isRectangleContainingRectangle(r, popupRectInScreen);
- }
-
- if (parent instanceof Frame) {
- return SwingUtilities.isRectangleContainingRectangle(((Component)parent).getBounds(), popupRectInScreen);
- }
- }
- }
-
- return false;
- }
-
- public void registerComponent(JComponent component) {
- ((Component)component).removeMouseListener(this);
- ((Component)component).addMouseListener(this);
- }
-
- public void setDismissDelay(int microSeconds) {
- this.insideTimer.setInitialDelay(microSeconds);
- }
-
- public void setEnabled(boolean flag) {
- this.enabled = flag;
- if (!flag) {
- this.hideTipWindow();
- }
-
- }
-
- public void setInitialDelay(int microSeconds) {
- this.enterTimer.setInitialDelay(microSeconds);
- }
-
- public void setLightWeightPopupEnabled(boolean aFlag) {
- this.lightWeightPopupEnabled = aFlag;
- }
-
- public void setReshowDelay(int microSeconds) {
- this.exitTimer.setInitialDelay(microSeconds);
- }
-
- public static ToolTipManager sharedInstance() {
- return sharedInstance;
- }
-
- void showTipWindow() {
- if (this.insideComponent != null && this.insideComponent.isShowing()) {
- if (this.enabled) {
- Point screenLocation = this.insideComponent.getLocationOnScreen();
- Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
- Point location = new Point();
- this.hideTipWindow();
- this.tip = this.insideComponent.createToolTip();
- this.tip.setTipText(this.toolTipText);
- Dimension size = this.tip.getPreferredSize();
- if (this.lightWeightPopupEnabled) {
- this.tipWindow = new JPanelPopup(this, this.tip, size);
- } else {
- this.tipWindow = new PanelPopup(this, this.tip, size);
- }
-
- this.tipWindow.addMouseListener(this);
- if (this.preferredLocation != null) {
- location.x = screenLocation.x + this.preferredLocation.x;
- location.y = screenLocation.y + this.preferredLocation.y;
- } else {
- location.x = screenLocation.x + this.mouseEvent.getX();
- location.y = screenLocation.y + this.mouseEvent.getY() + 20;
- if (location.x + size.width > screenSize.width) {
- location.x -= size.width;
- }
-
- if (location.y + size.height > screenSize.height) {
- location.y -= size.height + 20;
- }
- }
-
- if (this.popupRect == null) {
- this.popupRect = new Rectangle(location.x, location.y, this.tipWindow.getBounds().width, this.tipWindow.getBounds().height);
- } else {
- this.popupRect.setBounds(location.x, location.y, this.tipWindow.getBounds().width, this.tipWindow.getBounds().height);
- }
-
- if (!this.popupFit(this.popupRect, this.insideComponent)) {
- this.tipWindow = new WindowPopup(this, frameForComponent(this.insideComponent), this.tip, size);
- }
-
- this.tipWindow.show(this.insideComponent, location.x, location.y);
- this.insideTimer.start();
- this.timerEnter = System.currentTimeMillis();
- this.tipShowing = true;
- }
-
- }
- }
-
- public void unregisterComponent(JComponent component) {
- ((Component)component).removeMouseListener(this);
- }
- }
-